using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class LocalLogicServerSession : ISession
    {
        private class MainThreadExecutor : Singleton<MainThreadExecutor>, IExecutor
        {
            private MainThreadExecutor() { }
            public void Execute(Action action)
            {
                ThreadHelper.Instance.CallInMainThread(action);
            }
        }

        public IExecutor GetExecutor()
        {
            //因为服务器发消息是异步序列化的, 所以这里丢到队列异步执行
            return MainThreadExecutor.Instance;
        }

        public bool SendPack(SendPackage pack)
        {
            ReceivePackage receivePackage = ConcurrentClassPool<ReceivePackage>.Get();
            try
            {
                pack.FinishWrite();
                int size = pack.OutputStream.Size;
                byte[] data = pack.OutputStream.Buf;

                receivePackage.Init(data, 0, size);
                LocalLogicClientSession localLogicClientSession = LocalLogicManager.Instance.localLogicClientSession;
                return CGameNetHandler.Instance.ProcessPackage(localLogicClientSession, receivePackage);
            }
            finally
            {
                ConcurrentClassPool<SendPackage>.Put(pack);
                ConcurrentClassPool<ReceivePackage>.Put(receivePackage);
            }

        }

        public void Close(bool active = false)
        {
        }

        public void Disconnect()
        {
        }

    }
}
